﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Store;
using System.ComponentModel;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Search;

namespace ObjectSearch
{
    public interface IIndexSettings
    {
        Analyzer Analyzer { get; set; }
        Directory Directory { get; set; }
        Lucene.Net.Search.Similarity Similarity { get; set; }
    }

    public class IndexSettings : IIndexSettings, System.ComponentModel.INotifyPropertyChanged
    {
        public IndexSettings()
        {
            string tempIndex = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Index");

            if (!System.IO.Directory.Exists(tempIndex))
                System.IO.Directory.CreateDirectory(tempIndex);

            _Analyzer = new StandardAnalyzer();

            _Directory = FSDirectory.GetDirectory(tempIndex, false);

            _Similarity = DefaultSimilarity.GetDefault();
       }

        #region IIndexSettings Members

        /// <summary>
        /// A private field used to hold the value for the <see cref="Analyzer"/> property.
        /// </summary>
        private Lucene.Net.Analysis.Analyzer _Analyzer;

        public Lucene.Net.Analysis.Analyzer Analyzer
        {
            get
            {
                return _Analyzer;
            }
            set
            {
                if (_Analyzer != value)
                {
                    _Analyzer = value;
                    OnPropertyChanged("Analyzer");
                }
            }
        }

        /// <summary>
        /// A private field used to hold the value for the <see cref="Directory"/> property.
        /// </summary>
        private Lucene.Net.Store.Directory _Directory;

        public Lucene.Net.Store.Directory Directory
        {
            get
            {
                return _Directory;
            }
            set
            {
                if (_Directory != value)
                {
                    _Directory = value;
                    OnPropertyChanged("Directory");
                }
            }
        }

        /// <summary>
        /// A private field used to hold the value for the <see cref="Similarity"/> property.
        /// </summary>
        private Lucene.Net.Search.Similarity _Similarity;

        public Lucene.Net.Search.Similarity Similarity
        {
            get
            {
                return _Similarity;
            }
            set
            {
                if (_Similarity != value)
                {
                    _Similarity = value;
                    OnPropertyChanged("Similarity");
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Fires the <see cref="E:PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName">The name of the property that has changed.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);

                this.PropertyChanged(this, args);

            }
        }

        /// <summary>
        /// Occurs when a property value has changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }


}
